"use strict";
cc._RF.push(module, 'ae342n+CHxBnJlBenvKJoMj', 'event');
// script/poker_project/base/event.ts

"use strict";
/**
 * 事件管理器
 */
Object.defineProperty(exports, "__esModule", { value: true });
var _Event = /** @class */ (function () {
    function _Event() {
        this.subscribe = {};
        this.m_emit_reference_count = 0;
    }
    /**
     * @description add listener
     * @public
     * @param name  name listener
     * @param func function for call
     * @param target unxubscribe function
     */
    _Event.prototype.on = function (name, func, target) {
        var _this = this;
        if (!this.subscribe[name]) {
            this.subscribe[name] = [];
        }
        this.subscribe[name].push({ func: func, del: false, target: target });
        return function () {
            _this.off(name, func);
        };
    };
    /**
     * @description like on but only call once
     * @public
     * @param name  name listener
     * @param func function for call
     */
    _Event.prototype.once = function (name, func, target) {
        var unsubscribe = undefined;
        unsubscribe = this.on(name, function () {
            var args = [];
            for (var _i = 0; _i < arguments.length; _i++) {
                args[_i] = arguments[_i];
            }
            func.apply(target, args);
            unsubscribe();
        }, target);
        return unsubscribe;
    };
    /**
     * @description dispatch event
     * @public
     * @param name  name listener
     * @param func function for call
     */
    _Event.prototype.emit = function (name) {
        var args = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            args[_i - 1] = arguments[_i];
        }
        ++this.m_emit_reference_count;
        if (this.subscribe[name]) {
            this.subscribe[name].forEach(function (item) {
                if (!item.del && item.func) {
                    item.func.apply(item.target, args);
                }
            });
        }
        --this.m_emit_reference_count;
        return;
    };
    /**
     * @description remove listener
     * @public
     * @param name  name listener
     * @param func function for call
     */
    _Event.prototype.off = function (name, func) {
        if (this.subscribe[name]) {
            if (func) {
                this.subscribe[name].forEach(function (item) {
                    if (item.func === func) {
                        item.del = true;
                    }
                });
            }
            else {
                this.subscribe[name].forEach(function (item) {
                    item.del = true;
                });
            }
        }
        if (this.m_emit_reference_count === 0) {
            this.clear();
        }
    };
    /**
     * @description remove all listener
     * @public
     */
    _Event.prototype.clear = function () {
        for (var name in this.subscribe) {
            this.subscribe[name] = this.subscribe[name].filter(function (item) { return !item.del; });
        }
    };
    return _Event;
}());
exports.default = _Event;

cc._RF.pop();